//
//  UILabel+ALStyledLabel.m
//  Tour3D
//
//  Created by peijing on 10/23/13.
//  Copyright (c) 2013 Shanghai Internet Scenes Technologies Co., Ltd. All rights reserved.
//

#import "UILabel+ALStyledLabel.h"


@implementation UILabel (ALStyledLabel)

- (void)setLineSpace:(CGFloat)lineSpace {
    if([[[UIDevice currentDevice] systemVersion] floatValue] >= 7 && self.text) {
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        paragraphStyle.lineSpacing = lineSpace;
        NSDictionary *ats = @{
            NSFontAttributeName : self.font,
            NSForegroundColorAttributeName : self.textColor,
            NSParagraphStyleAttributeName : paragraphStyle
        };
        self.attributedText = [[NSAttributedString alloc] initWithString: self.text attributes: ats];
    }
}

- (void)setParagraphSpace:(CGFloat)lineSpace {
    if([[[UIDevice currentDevice] systemVersion] floatValue] >= 7 && self.text) {
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        paragraphStyle.paragraphSpacing = lineSpace;
//        paragraphStyle.lineSpacing = lineSpace;
        NSDictionary *ats = @{
                              NSFontAttributeName : self.font,
                              NSForegroundColorAttributeName : self.textColor,
                              NSParagraphStyleAttributeName : paragraphStyle
                              };
        self.attributedText = [[NSAttributedString alloc] initWithString: self.text attributes: ats];
    }
}

- (void)setLineSpace:(CGFloat)lineSpace alignment:(NSTextAlignment)alignment{
    if([[[UIDevice currentDevice] systemVersion] floatValue] >= 7 && self.text) {
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        paragraphStyle.lineSpacing = lineSpace;
        paragraphStyle.alignment = alignment;
        NSDictionary *ats = @{
                              NSFontAttributeName : self.font,
                              NSForegroundColorAttributeName : self.textColor,
                              NSParagraphStyleAttributeName : paragraphStyle
                              };
        self.attributedText = [[NSAttributedString alloc] initWithString: self.text attributes: ats];
    }

}

- (CGFloat)calculateAttributedTextHeight {
    CGSize bodySize = [self calculateAttributedTextSize];
    return bodySize.height;
}

- (CGSize)calculateAttributedTextSize {
    CGSize bodySize = [self.attributedText boundingRectWithSize: CGSizeMake(self.width, FLT_MAX) options: NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context: nil].size;
    return bodySize;
}

@end
